import * as React from 'react';
import axios, { AxiosRequestConfig } from 'axios';
import { Column } from '../../../../common/ducks/GridPage';
import { Button, Text } from 'tea-component';
import { InstanceItem, ServiceStatusName, ServiceStatusTheme } from '../../types';
import formatDate from '@src/common/util/formatDate';
import {DuckCmpProps} from "saga-duck";
import baseinfoPageduck from './PageDuck'
import {instancesUpDown} from '@src/config'

export default (
    {
      duck: { creators, selectors },
      dispatch,
      store,
    }: DuckCmpProps<baseinfoPageduck>): Column<InstanceItem>[] =>{
  return [
    {
      key: 'id',
      header: '实例ID',
      render: x => (
          <Text tooltip={x.id} overflow>
            {x.id}
          </Text>
      ),
    },
    {
      key: 'status',
      header: '状态',
      render: x => (
          <Text theme={ServiceStatusTheme[x.status]} tooltip={ServiceStatusName[x.status]}>
            {ServiceStatusName[x.status] || '-'}
          </Text>
      ),
    },
    {
      key: 'serviceVersion',
      header: '服务版本',
      render: x => <Text tooltip={x.serviceVersion}>{x.serviceVersion || '-'}</Text>,
    },
    {
      key: 'ip',
      header: 'IP 地址',
      render: x => <Text tooltip={x.host}>{x.host || '-'}</Text>,
    },
    {
      key: 'port',
      header: '端口',
      render: x => <Text tooltip={x.port}>{x.port ?? '-'}</Text>,
    },
    {
      key: 'clientVersion',
      header: '客户端版本',
      render: x => <Text tooltip={x.clientVersion}>{x.clientVersion || '-'}</Text>,
    },
    {
      key: 'update',
      header: '上一次更新时间',
      render: x => <Text tooltip={formatDate(x.lastUpdateTime)}>{formatDate(x.lastUpdateTime) || '-'}</Text>,
    },
    {
      key: 'operation',
      header: '操作',
      render: x => (
          <>
            {x.status==='UP'?
                <Button onClick={async () => {
                  const token=window.sessionStorage.getItem('atom_login_token')
                  const res=await axios.put(`${instancesUpDown}/atom/v1/service/instancesUpDown`,
                      {serviceName:x.serviceName,ephemeral:true,weight:1,clusterName:'DEFAULT',groupName:'DEFAULT_GROUP',namespaceId:x.namespaceId,ip:x.host,port:x.port,enabled:false},
                      {headers: {
                          ...(token
                              ? {
                                token
                              }
                              : {}),
                        }}
                  )
                  if(res.status === 200){
                    dispatch(creators.reload())
                  }
                }}>下线</Button>:
                <Button onClick={async () => {
                  const token=window.sessionStorage.getItem('atom_login_token')
                  const res =await axios.put(`${instancesUpDown}/atom/v1/service/instancesUpDown`,
                      {serviceName:x.serviceName,ephemeral:true,weight:1,clusterName:'DEFAULT',groupName:'DEFAULT_GROUP',namespaceId:x.namespaceId,ip:x.host,port:x.port,enabled:true},
                      {headers: {
                          ...(token
                              ? {
                                token,
                              }
                              : {}),
                        }}
                  )
                  if(res.status === 200){
                    dispatch(creators.reload())
                  }
                }}>上线</Button>}
          </>
      ),
    },
  ];
}